<template>
  <div class="profile-container">

    <!-- 未登录的盒子   -->
    <div class="not-login  header-bj" v-if="!user">
      <img src="@/assets/mobile.png" alt="" @click="$router.push('/login')">
      <span>登录/注册</span>
    </div>
    <!-- /未登录的盒子   -->

    <!--    已登录盒子-->
    <div class="login-box header-bj" v-else>
      <div class="user-info">
        <div class="left">
          <van-image
              round
              fit="cover"
              :src="users.photo"
          />
          <span class="name">{{ users.name }}</span>
        </div>
        <van-button type="default" to="/user">编辑资料</van-button>
      </div>

      <!--      用户数据-->
      <div class="user-data">
        <div class="data-item">
          <span>{{ users.art_count }}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{ users.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{ users.fans_count }}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{ users.like_count }}</span>
          <span>获赞</span>
        </div>
      </div>
      <!--     / 用户数据-->

    </div>
    <!--    /已登录盒子-->

    <!--    收藏/历史-->
    <van-grid clickable :column-num="2">
      <van-grid-item icon="home-o" text="收藏" to="/collect">
        <template #icon>
          <i class="toutiao toutiao-shoucang"> </i>
        </template>
      </van-grid-item>
      <van-grid-item icon="search" text="历史" to="/history">
        <template #icon>
          <i class="toutiao toutiao-lishi"> </i>
        </template>
      </van-grid-item>
    </van-grid>
    <!--    / 收藏/历史-->

    <!--    ceil-->
    <div class="margin-box">
      <van-cell title="消息通知" is-link/>
      <van-cell title="小智同学" is-link/>
    </div>
    <van-cell class="logout-btn" title="退出" @click="onLogout" v-if="user"/>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { Dialog, Toast } from 'vant'
import { getUserInfo } from '@/api/user'

export default {
  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },
  name: 'Profile',
  data () {
    return {
      users: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    // 弹出框
    onLogout () {
      Dialog.confirm({
        title: '黑马头条',
        message: '确认退出登录'
      })
        .then(() => {
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
        })
    },
    async loadUserInfo () {
      try {
        const res = await getUserInfo()
        this.users = { ...res }
      } catch (e) {
        Toast('系统异常')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.profile-container {
  // 头部背景
  .header-bj {
    width: 100%;
    height: 401px;
    background-image: url("~@/assets/banner.png");
    background-size: 100%;
  }

  // 未登录显示盒子
  .not-login {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    img {
      width: 132px;
      height: 132px;
    }

    span {
      font-size: 28px;
      color: #fff;
      margin-top: 15px;
    }

  }

  // 已登录盒子
  .login-box {
    overflow: hidden;

    // 用户信息
    .user-info {
      display: flex;
      justify-content: space-between;
      margin-top: 116px;
      height: 132px;
      align-items: center;
      padding: 0 32px;

      // 左侧头像和用户名字
      .left {
        display: flex;
        align-items: center;

        .van-image {
          width: 132px;
          height: 132px;
        }

        .name {
          color: #fff;
          font-size: 30px;
          margin-left: 22px;
        }
      }

      // 编辑按钮
      .van-button {
        width: 115px;
        height: 32px;
        font-size: 20px;
        color: #666;
        padding: 0;
        border-radius: 16px;
      }
    }

    // 用户数据
    .user-data {
      height: 153px;
      display: flex;

      //数据每一项
      .data-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        color: #ffffff;
        flex: 1;

        span:nth-child(1) {
          font-size: 26px;
        }

        span:nth-child(2) {
          font-size: 23px;
          margin-top: 15px;
        }
      }

    }

  }

  // 历史 /收藏
  .van-grid {
    .toutiao {
      font-size: 45px;
    }

    .toutiao-shoucang {
      color: #EB5253;
    }

    .toutiao-lishi {
      color: #FF9D1D;
    }

    /deep/ .van-grid-item__text {
      font-size: 27px;
      color: #333;
      margin-top: 16px;
      letter-spacing: 20px;
      padding-left: 20px
    }
  }

  // 退出按钮
  .logout-btn {
    font-size: 29px;
    color: #d86262;
    text-align: center;
  }

  .margin-box {
    margin: 10px 0;
  }
}
</style>
